//index.js
//获取应用实例

const app=getApp();
const log =require('../../log.js');
const util=require('../../utils/util.js');
Page({
  data: {
    time:3,
    showAdver:true,
    interval:"",
    showAdTime:false
   // logined:0,
  },

  onLoad: function (options) {
    var date=util.formatTime(new Date());
    log.info("Time:"+date+",index:login,options:"+JSON.stringify(options)); 
    console.log('index')
    let that=this;
    this.getLoginImage();
    that.wxLogin();

  },
  wxLogin:function(){
    let that=this;
    var open=wx.getStorageSync('open');  
    console.log(open);
    if(open!="" && open!=undefined){
      var member_id=open.member_id
      //var member_id=0;
      console.log(member_id);
      if(member_id){
        //console.log(member_id);
        wx.reLaunch({
          url: '/pages/default/index',
        })
      }else{
        wx.reLaunch({
          url: '/pages/default/index',
        })
      }
    }else{     
        console.log('no info')      
        wx.login({
            success: res => {
          var code = res.code; 
          console.log('code='+code)         
         
             wx.request({
                url: app.globalData.domain+'wechat/login/getUnionId',
                data: { code: code },
                success: (res) => {
                  console.log(res.data);
                  //console.log(res.data.unionid);
                 // wx.setStorageSync('open', res.data)
                 var data=res.data;
                 if(data.id){
                   wx.setStorageSync('open', res.data);        
                } 
                var member_id=data.member_id;
                
                  wx.reLaunch({
                    url: '/pages/default/index',
                  })
               
                }
              })
            }})

         

        
      
        }
  }
  ,
  getLoginImage:function(){
    let that = this;
    wx.request({
      url: app.globalData.domain+'wechat/login/getAdver',
      data:{from:1},
      success:res=>{
        that.setData({
          loginImage:res.data.image
        })
      }
  })
  },
showAdvert:function(){
      this.setData({
        showAdTime:true
      });
      let that = this;
     　　// 在进入页面时开始执行定时器，每秒执行一次miao-1操作。
     　　that.timer = setInterval(function() {
     　　　　that.setData({
     　　　　time: that.data.time - 1
     　　　　})
     
     　　// 当miao==0时，清除定时器clearInterval(this.time)（一定要清除定时器），然后自动跳转到首页
     
     　　if (that.data.time == 0) {
     　　　　console.log(that.data.time)
     　　　　clearInterval(that.timer);
     　　　that.setData({
             showAdver:false
             })
             wx.reLaunch({
                url: '/pages/default/index',
              })
     　　}
     
     　}, 1000)
     
       },
       
  cliadv: function() {
     　　clearInterval(this.timer)
     　   this.setData({
             showAdver:false
           })
           wx.reLaunch({
            url: '/pages/default/index',
          })
     },
})
